﻿#ifndef __TIMEVALUE_h__
#define __TIMEVALUE_h__

#include <ctime>
#include <string>


static int32_t g_timeOffSet = 0;//时间偏移值(秒)
class TimeValue
{
public:
	static const int USECS;
	static const int SECOND;
	static const int MINUTE;
	static const int HOUR;
	static const int DAY;
	static const int WEEK;

public:
	static const TimeValue ZERO;

	static std::time_t GetTime();
	static std::time_t GetMsTime();
	static std::string GetCurTimeStr(const char *normatStr = "%Y-%m-%d %H:%M:%S");
	static std::string GetCurTimeMsStr(const char *normatStr = "%Y-%m-%d %H:%M:%S");
	static void ParseTime(const char *str, unsigned int &time);
	static void ParseTime(const char *str, std::time_t &time);
	static std::time_t GetZeroTime(std::time_t time);
	static std::time_t GetTodayZero();
	static bool IsSameDay(std::time_t l, std::time_t r);
	static bool IsSameWeek(std::time_t l, std::time_t r, int offsetSec = 0);
	static bool IsSameMonth(std::time_t l, std::time_t r, int offsetSec = 0);
	static bool ExceedInterval(std::time_t target, int interval);
	static int GetDay(std::time_t time = GetTime());
	static int GetMonth(std::time_t time = GetTime());
	static int GetYear(std::time_t time = GetTime());
	static int GetWeek(std::time_t time = GetTime());
	static int GetHour(std::time_t time = GetTime());
	static int GetMin(std::time_t time = GetTime());
	static bool IsBetween(int beginHour, int beginMin, int endHour, int endMin, std::time_t curTime);
	static bool IsCurMonth(int year, int month, int offsetSec = 0);


	static std::time_t GetDayStart(std::time_t time, int offsetSec = 0);
	static time_t GetDayStart(int year, int month, int day);
	//周一0点开始为起始时间,offsetSec偏移秒数
	static std::time_t GetWeekStart(std::time_t time, int offsetSec = 0);
	static std::time_t GetMonthStart(std::time_t time, int offsetSec = 0);
	static std::time_t GetMinuteStart(std::time_t time, int offsetSec = 0);
	static bool IsLeapYear(int year);
	// 获取两个时间相差多少天， t1 < t2 为负值
	static int GetDiffDay(std::time_t t1, std::time_t t2, int offsetSec = 0);

	friend bool operator > (const TimeValue &tv1, const TimeValue &tv2);
	friend bool operator < (const TimeValue &tv1, const TimeValue &tv2);
	friend bool operator >= (const TimeValue &tv1, const TimeValue &tv2);
	friend bool operator <= (const TimeValue &tv1, const TimeValue &tv2);
	friend TimeValue operator - (const TimeValue &tv1, const TimeValue &tv2);
	friend TimeValue operator + (const TimeValue &tv1, const TimeValue &tv2);
	friend bool operator == (const TimeValue &tv1, const TimeValue &tv2);
	friend bool operator != (const TimeValue &tv1, const TimeValue &tv2);
	TimeValue &operator += (const TimeValue &tv);
	TimeValue &operator -= (const TimeValue &tv);

	TimeValue();
	TimeValue(std::time_t msTime);
	~TimeValue();

	bool IsTimeout(std::time_t timeStamp) const;
	bool IsTimeout(const TimeValue &t) const;

	bool timeup(std::time_t t, int interval);
public:
	inline void SetMsTime(std::time_t msTime)
	{
		tv_ = msTime;
	}

	inline void SetTime(std::time_t timeStamp)
	{
		tv_ = timeStamp * 1000;
	}

	inline std::time_t sec(void) const
	{
		return tv_ / 1000;
	}

	inline void sec(std::time_t sec)
	{
		tv_ = sec * 1000;
	}

	inline std::time_t usec(void) const
	{
		return tv_;
	}

	inline void usec(std::time_t usec)
	{
		tv_ = usec;
	}

	inline void set(std::time_t sec, std::time_t ms = 0)
	{
		tv_ = sec * 1000 + ms;
	}

	inline void add(std::time_t sec, std::time_t ms = 0)
	{
		tv_ = tv_ + sec * 1000 + ms;
	}

	inline int dif(std::time_t subms)
	{
		return subms - tv_;
	}

	inline int dif()
	{
		return TimeValue::GetMsTime() - tv_;
	}

private:
	//ms time
	std::time_t tv_ = 0;
};

#endif // __TIMEVALUE_h__